/*
	background page for FDU Go
	==========================
	stores local database
	manages local storage
	record closed tabs activity
*/

// local storage manager
var lsManager = {
	// check existence
	exist : function (id) {
		if (localStorage[id]) {
			return true;
		} else {
			return false;
		}
	},
	// remove
	remove : function (id) {
		localStorage[id] && localStorage.removeItem(id);
	},
	// save to local storage 
	save : function (id, data) {
		if (typeof(data) == 'string' || typeof(data) == 'number') {
			localStorage[id] = data;
		} else if (typeof(data) == 'boolean') {
			localStorage[id] = data ? '1':'0';
		} else {
			localStorage[id] = JSON.stringify(data);
		}
	},
	// update existing list
	// if not exist creat a new one, if found existing item update it and bring it front
	// specify limit to limit the maxium length of list
	// idattr is used to distinguish one item for another, often it's title or url
	updateList : function (id, new_item, idattr, limit) {
		var list = localStorage[id];
		var new_idattr = new_item[idattr];
		var list_length;
		if (list) {
			list = JSON.parse(list);
			list_length = list.length;
			// see if there is already one there
			// if so, update it
			for (var i=0;i<list_length;i++) {
				if (list[i][idattr] == new_idattr) {
					list.splice(i,1);
					list.unshift(new_item);
					localStorage[id] = JSON.stringify(list);
					return;
				}
			}
			// we got a new item, add it to the list!
			list.unshift(new_item);
			if (limit) {
				list = list.slice(0, limit);
			}
		} else {
			list = [new_item];
		}
		localStorage[id] = JSON.stringify(list);
	},
	
	// load from local storage
	// if isObject is not specified, load as string
	get : function (id, isObject) {
		if (isObject) {
			var res;
			try {
				res = JSON.parse(localStorage[id]);
			} catch (err) {
				res = undefined;
			}
			return res;
		} else {
			return localStorage[id];
		}
	}
};

// local database
var localDB = {};
localDB.transferDB = [
	{title:"复旦大学首页", pyshort:"fddxsy", url:"http://www.fudan.edu.cn"},
	{title:"复旦大学图书馆", pyshort:"fddxtsg", url:"http://www.library.fudan.edu.cn/"},
	{title:"复旦大学收费服务平台", pyshort:"fddxsffwpt", url:"http://payment.fudan.edu.cn/"},
	{title:"复旦大学教务处", pyshort:"fddxjwc", url:"http://www.jwc.fudan.edu.cn/"},
	{title:"复旦大学保卫处", pyshort:"fddxbwc", url:"http://10.63.70.2/"},
	{title:"复旦大学团委-青年复旦网", pyshort:"fddxtwqnfdw", url:"http://www.youth.fudan.edu.cn/"},
	{title:"复旦大学学生会", pyshort:"fddxxsh", url:"http://su.fudan.edu.cn/"},
	{title:"复旦大学信息化办公室", pyshort:"xxbfddxxxhbgs", url:"http://www.ecampus.fudan.edu.cn/"},
	{title:"复旦大学外事处暨港澳台事务办公室", pyshort:"fddxwscjgatswbgs", url:"http://www.fao.fudan.edu.cn/"},
	{title:"复旦出版社", pyshort:"fdcbs", url:"http://www.fudanpress.com.cn/"},
	{title:"复旦大学英语教学网站", pyshort:"fddxyyjxwz", url:"http://cec.fudan.edu.cn/"},
	{title:"复旦大学通识教育核心课程", pyshort:"fddxtsjyhxkc", url:"http://hexin.fudan.edu.cn/hexin/"},
	{title:"本科生学术研究资助计划UROP", pyshort:"bksxsyjzzjhurop", url:"http://www.fdurop.fudan.edu.cn/"},
	{title:"日月光华bbs", pyshort:"ryghbbs", url:"http://bbs.fudan.edu.cn/"},
	{title:"复旦邮箱", pyshort:"fdyx", url:"http://mail.fudan.edu.cn"},
	{title:"复旦pt", pyshort:"fdpt", url:"http://pt.vm.fudan.edu.cn/"},
	{title:"书院生活", pyshort:"sysh", url:"http://10.107.0.51/life/"},
	{title:"复旦大学现代教育技术中心", pyshort:"fddxxdjyjszx", url:"http://10.64.130.4/"},
	{title:"软件下载", pyshort:"rjxz", url:"http://10.64.130.17:82/"},
	{title:"urp系统-信息化校园应用系统", pyshort:"urpxtxxhxyyyxt", url:"http://www.urp.fudan.edu.cn"},
	{title:"校园信息门户portal", pyshort:"xyxxmhportal", url:"http://portal.fudan.edu.cn"},
	{title:"图书借阅信息查询", pyshort:"tsjyxxcx", url:"http://202.120.227.6"},
	{title:"个人主页系统", pyshort:"grzyxt", url:"http://homepage.fudan.edu.cn"},
	{title:"选课系统", pyshort:"xkxt", url:"http://xk.fudan.edu.cn"},
	{title:"虚拟校园", pyshort:"xnxy", url:"http://elearning.fudan.edu.cn"},
	{title:"网络课堂", pyshort:"wlkt", url:"http://202.120.227.42/"},
	{title:"校园一卡通综合查询系统", pyshort:"xyyktzhcxxt", url:"http://www.ecard.fudan.edu.cn"},
	{title:"体育成绩查询系统", pyshort:"tycjcxxt", url:"http://www.fdty.fudan.edu.cn/sportscore/"},
	{title:"数学分析课程网站", pyshort:"sxfxkcwz", url:"http://math.fudan.edu.cn/math_anal/"},
	{title:"高等数学课程网站", pyshort:"gdsxkcwz", url:"http://math.fudan.edu.cn/gdsx/"},
	{title:"校车时刻表", pyshort:"xcskb", url:"http://www.xyfw.fudan.edu.cn/s/55/t/190/55/38/info21816.htm"},
	{title:"复旦地图", pyshort:"fddt", url:"http://map.fudan.edu.cn/"},
	{title:"复旦学生网", pyshort:"fdxsw", url:"http://stu.fudan.edu.cn"},
	{title:"新生入学指南", pyshort:"xsrxzn", url:"http://stu.fudan.edu.cn/freshman"},
	{title:"活动平台", pyshort:"hdpt", url:"http://stu.fudan.edu.cn/event"},
	{title:"许愿墙", pyshort:"xyq", url:"http://stu.fudan.edu.cn/wish"},
	{title:"讲座信息平台", pyshort:"jzxxpt", url:"http://stu.fudan.edu.cn/lecture"},
	{title:"艺术教育中心", pyshort:"ysjyzx", url:"http://www.yjzx.fudan.edu.cn/"},
	{title:"复旦学院", pyshort:"fdxy", url:"http://www.fdcollege.fudan.edu.cn/"},
	{title:"中国语言文学系", pyshort:"zgyywxx", url:"http://chinese.fudan.edu.cn/"},
	{title:"历史系", pyshort:"lsx", url:"http://history.fudan.edu.cn"},
	{title:"哲学学院", pyshort:"zxxy", url:"http://philosophy.fudan.edu.cn/"},
	{title:"旅游学系", pyshort:"lyxx", url:"http://tourism.fudan.edu.cn"},
	{title:"经济学院", pyshort:"jjxy", url:"http://www.econ.fudan.edu.cn/"},
	{title:"国际关系与公共事务学院", pyshort:"gjgxyggswxy", url:"http://www.sirpa.fudan.edu.cn/"},
	{title:"法学院", pyshort:"fxy", url:"http://www.law.fudan.edu.cn/"},
	{title:"新闻学院", pyshort:"xwxy", url:"http://www.xwxy.fudan.edu.cn/"},
	{title:"外文学院", pyshort:"wwxy", url:"http://dfll.fudan.edu.cn/"},
	{title:"社会发展与公共政策学院", pyshort:"shfzyggzcxy", url:"http://www.ssdpp.fudan.edu.cn/"},
	{title:"数学科学学院", pyshort:"sxkxxy", url:"http://math.fudan.edu.cn/"},
	{title:"物理系", pyshort:"wlx", url:"http://www.physics.fudan.edu.cn"},
	{title:"化学系", pyshort:"hxx", url:"http://www.chemistry.fudan.edu.cn/"},
	{title:"高分子科学系", pyshort:"gfzkxx", url:"http://www.polymer.fudan.edu.cn/"},
	{title:"环境科学与工程系", pyshort:"hjkxygcx", url:"http://environment.fudan.edu.cn/"},
	{title:"生命科学学院", pyshort:"smkxxy", url:"http://life.fudan.edu.cn/"},
	{title:"信息科学与工程学院", pyshort:"xxkxygcxy", url:"http://www.it.fudan.edu.cn/"},
	{title:"电子工程系", pyshort:"dzgcx", url:"http://ee.fudan.edu.cn"},
	{title:"光科学与工程系", pyshort:"gkxygcx", url:"http://www.optics.fudan.edu.cn/"},
	{title:"微电子学系", pyshort:"wdzxx", url:"http://me.fudan.edu.cn"},
	{title:"计算机科学技术学院", pyshort:"jsjkxjsxy", url:"http://www.cs.fudan.edu.cn"},
	{title:"材料科学系", pyshort:"clkxx", url:"http://mse.fudan.edu.cn/"},
	{title:"光源与照明工程系", pyshort:"gyyzmgcx", url:"http://light.fudan.edu.cn/"},
	{title:"力学与工程科学系", pyshort:"lxygckxx", url:"http://mech.fudan.edu.cn/"},
	{title:"管理学院", pyshort:"glxy", url:"http://www.fdsm.fudan.edu.cn/"},
	{title:"复旦大学上海医学院", pyshort:"fddxshyxy", url:"http://shmc.shmu.edu.cn"},
	{title:"公共卫生学院", pyshort:"ggwsxy", url:"http://sph.fudan.edu.cn/"},
	{title:"药学院", pyshort:"yxy", url:"http://spfdu.fudan.edu.cn/"},
	{title:"护理学院", pyshort:"hlxy", url:"http://nursing.fudan.edu.cn/"},
	{title:"软件学院", pyshort:"rjxy", url:"http://www.software.fudan.edu.cn/"},
	{title:"继续教育学院", pyshort:"jxjyxy", url:"http://www.cce.fudan.edu.cn/"},
	{title:"网络教育学院", pyshort:"wljyxy", url:"http://dec.fudan.edu.cn/"},
	{title:"国际文化交流学院", pyshort:"gjwhjlxy", url:"http://www.ies.fudan.edu.cn/"},
	{title:"复旦大学体育部", pyshort:"fddxtyb", url:"http://www.fdty.fudan.edu.cn/"},
	{title:"复旦大学人文社会科学网", pyshort:"fddxrwshkxw", url:"http://wkkyc.fudan.edu.cn/"},
	{title:"社会科学基础部", pyshort:"shkxjcb", url:"http://www.theory.fudan.edu.cn/"},
	{title:"复旦大学研究生院", pyshort:"fddxyjsy", url:"http://www.gs.fudan.edu.cn/"},
	{title:"物理系团学联", pyshort:"wlxtxl", url:"http://www.physics.fudan.edu.cn/tps/sites/phyusu/"},
	{title:"通信工程系", pyshort:"txgcx", url:"http://www.cse.fudan.edu.cn"}

];
localDB.help_db = [
	{title:'高级搜索开关列表：', additional:'这些开关都是以“-”开头的，正常搜索时不会触发它们', url:''},
	{title:'1. -help或-bz', additional:'显示本列表', url:''},
	{title:'2. -10或-sd', additional:'显示日月光华bbs今日十大', url:''},
	{title:'3. -lec或-jz', additional:'显示复旦学生网讲座信息平台首页讲座列表', url:''},
	{title:'4. -event或-hd', additional:'显示复旦学生网活动平台首页活动列表', url:''},
	{title:'5. -r', additional:'搜索最近关闭的标签页', url:''},
	{title:'6. ???', additional:'???', url:''},
	{title:'7. ???', additional:'???', url:''},
	{title:'8. ???', additional:'???', url:''},
	{title:'9. ???', additional:'???', url:''}
];
localDB.tabs_db = [];

var tabRecorder = (function () {
	var opening_tabs = {};
	var tab_limit = 50;
	var closed_tab_num;
	var closed_tab;
	var grabbed = false;
	if (!lsManager.exist('closedTabNum')) {
		closed_tab_num = 0;
		lsManager.save('closedTabNum', 0);
	} else {
		closed_tab_num = lsManager.get('closedTabNum');
	}
	// tab event handlers
	function onTabUpdate(tabID, changInfo, tab) {
		opening_tabs['tab_'+tabID] = {
			title: tab.title,
			url: tab.url
		};
	}

	function onTabClose(tabID, removeInfo) {
		var tab_info = opening_tabs['tab_'+tabID];
		if (!tab_info) {
			return;
		}
		var tab_url = tab_info.url;
		var tab_title = tab_info.title;
		var scheme_regex = /^(http:|https:|ftp:|file:|mms:)/;
		delete opening_tabs['tab'+tabID];
		// we don't want to record stuffs like chrome://
		if (tab_url && scheme_regex.test(tab_url)) {
			var now = (new Date()).getTime();
			var new_item = {
				title: tab_title,
				url: tab_url,
				time : now
			};
			grabbed = false;
			lsManager.updateList('closedTabList', new_item, 'url', tab_limit);
		}
	}
	chrome.tabs.onUpdated.addListener(onTabUpdate);
	chrome.tabs.onRemoved.addListener(onTabClose);
	
	var tabRecoder = {
		getClosedTabList : function () {
			if (!grabbed) {	
				closed_tabs = lsManager.get('closedTabList', true);
				grabbed = true;
			}
			return closed_tabs;
		},
		clearTabList : function () {
			lsManager.save('closedTabNum', 0);
			lsManager.save('closedTabList', []);
		}
	};
	return tabRecoder;
})();

